package com.yxw.system.controller;

import com.atguigu.model.system.SysRole;
import com.atguigu.model.vo.AssginRoleVo;
import com.atguigu.model.vo.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxw.common.result.Result;
import com.yxw.system.annotation.Log;
import com.yxw.system.enums.BusinessType;
import com.yxw.system.exception.YXWException;
import com.yxw.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @className: com.yxw.system.controller.SysRoleController
 * @description: TODO
 * @author: tommy
 * @create: 2023-07-21 下午 07:13
 */

@Api(tags = "管理员角色接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("获取用户的角色数据")
    @GetMapping("toAssign/{userId}")
    public Result toAssign(@PathVariable String userId) {
    Map<String,Object> roleMap =  sysRoleService.getRoleByUserId(userId);
    return Result.ok(roleMap);
    }

    @ApiOperation("根据用户分配角色")
    @PostMapping("doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }
    //7 批量删除
    // 多个id值[1, 2,3]
    // json数组格式 --- java的List集合，json对象格式对应 --- java的对象
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        sysRoleService.removeByIds(ids);
        return Result.ok();

    }

    //6 修改
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改")
    @PutMapping("update")
    public Result update(@RequestBody SysRole sysRole){
        boolean isSuccess = sysRoleService.updateById(sysRole);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5 根据id查询
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据ID查询")
    @PostMapping("get/{id}")
    public Result getById(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    //4 添加
    // @RequestBody 不能使用get提交方式
    // 传递json格式数据，把json格式数据封装到对象(SysRole)里面{....}
    @Log(title = "角色管理",businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result saveRole(@RequestBody SysRole sysRole) {
        boolean isSuccess = sysRoleService.save(sysRole);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }


    }

    //3 条件分页查询
    //page当前页 limit每页记录数
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result findPageQuerySysRole(@PathVariable Long page,
                                       @PathVariable Long limit,
                                       SysRoleQueryVo sysRoleQueryVo){
        // 创见page对象
        Page<SysRole> pageParam = new Page<>(page,limit);
        //调用service方法
    IPage<SysRole> pageModel =  sysRoleService.selectPage(pageParam,sysRoleQueryVo);
        //还回结果
        return Result.ok(pageModel);
    }


    //2 逻辑删除接口
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据ID移除管理员角色")
    @DeleteMapping("remove/{id}")
    public Result removeSysRole(@PathVariable Long id) {
        //调用方法删除
        boolean isSuccess = sysRoleService.removeById(id);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    //1 查询所有记录
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("查询所有管理员信息")
    @GetMapping("findAll")
    public Result findAllRole() {
        //TODO 模拟异常 ArithmeticException
//        try {
//            int i = 6/0;
//        } catch (Exception e) {
//            //手动抛出异常
//            throw new YXWException(20001,"运算符出错了!");
//        }
        //调用service
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }
}
